field types
pydantic 尽可能的使用标准库类型(standard library types)来标注字段来提供一个平滑的学习曲线;不过它也实现了许多常用的类型(commonly used types)
你当然可以创造属于自己的类型(your own pydantic-compatible types)
标准库类型
-
None
,type(None)
orLiteral[None]
- 只允许
None
值
- 只允许
-
bool
- 详见
Booleans
- 详见
-
int
- pydantic 使用
int(v)
去将 v 强制转换成int
- pydantic 使用
-
float
- 同样地,
float(v)
也会将 v 强制转换成float
- 同样地,
-
str
str
原样接收int
float
和Decimal
会被str(v)
强制转换bytes
和bytearray
会用v.decode()
强制转换- 继承自
str
的enum
会被v.value
强制转换 - 其他的类型都会报错
-
bytes
bytes
原样接收bytearray
用bytes(v)
转换str
用v.encode()
转换int
,float
和Decimal
用str(v).encode()
强制转换
-
list
允许
list
,tuple
,set
,frozenset
,deque
, 或者生成器(generators)并且将其转换为 list -
tuple
allows
list
,tuple
,set
,frozenset
,deque
, or generators and casts to a tuple; seetyping.Tuple
below for sub-type constraints -
dict
dict(v)
is used to attempt to convert a dictionary; seetyping.Dict
below for sub-type constraints -
set
allows
list
,tuple
,set
,frozenset
,deque
, or generators and casts to a set; seetyping.Set
below for sub-type constraints -
frozenset
allows
list
,tuple
,set
,frozenset
,deque
, or generators and casts to a frozen set; seetyping.FrozenSet
below for sub-type constraints -
deque
allows
list
,tuple
,set
,frozenset
,deque
, or generators and casts to a deque; seetyping.Deque
below for sub-type constraints -
datetime.date
see Datetime Types below for more detail on parsing and validation
-
datetime.time
see Datetime Types below for more detail on parsing and validation
-
datetime.datetime
see Datetime Types below for more detail on parsing and validation
-
datetime.timedelta
see Datetime Types below for more detail on parsing and validation
-
typing.Any
allows any value including
None
, thus anAny
field is optional -
typing.Annotated
allows wrapping another type with arbitrary metadata, as per PEP-593. The
Annotated
hint may contain a single call to theField
function, but otherwise the additional metadata is ignored and the root type is used. -
typing.TypeVar
constrains the values allowed based on
constraints
orbound
, see TypeVar -
typing.Union
see Unions below for more detail on parsing and validation
-
typing.Optional
Optional[x]
is simply short hand forUnion[x, None]
; see Unions below for more detail on parsing and validation and Required Fields for details about required fields that can receiveNone
as a value. -
typing.List
-
typing.Tuple
-
subclass of typing.NamedTuple
Same as
tuple
but instantiates with the given namedtuple and validates fields since they are annotated. See Annotated Types below for more detail on parsing and validation -
subclass of collections.namedtuple
Same as
subclass of typing.NamedTuple
but all fields will have typeAny
since they are not annotated -
typing.Dict
see Typing Iterables below for more detail on parsing and validation
-
subclass of typing.TypedDict
Same as
dict
but pydantic will validate the dictionary since keys are annotated. See Annotated Types below for more detail on parsing and validation -
typing.Set
see Typing Iterables below for more detail on parsing and validation
-
typing.FrozenSet
see Typing Iterables below for more detail on parsing and validation
-
typing.Deque
see Typing Iterables below for more detail on parsing and validation
-
typing.Sequence
see Typing Iterables below for more detail on parsing and validation
-
typing.Iterable
this is reserved for iterables that shouldn't be consumed. See Infinite Generators below for more detail on parsing and validation
-
typing.Type
see Type below for more detail on parsing and validation
-
typing.Callable
see Callable below for more detail on parsing and validation
-
typing.Pattern
will cause the input value to be passed to
re.compile(v)
to create a regex pattern -
ipaddress.IPv4Address
simply uses the type itself for validation by passing the value to
IPv4Address(v)
; see Pydantic Types for other custom IP address types -
ipaddress.IPv4Interface
simply uses the type itself for validation by passing the value to
IPv4Address(v)
; see Pydantic Types for other custom IP address types -
ipaddress.IPv4Network
simply uses the type itself for validation by passing the value to
IPv4Network(v)
; see Pydantic Types for other custom IP address types -
ipaddress.IPv6Address
simply uses the type itself for validation by passing the value to
IPv6Address(v)
; see Pydantic Types for other custom IP address types -
ipaddress.IPv6Interface
simply uses the type itself for validation by passing the value to
IPv6Interface(v)
; see Pydantic Types for other custom IP address types -
ipaddress.IPv6Network
simply uses the type itself for validation by passing the value to
IPv6Network(v)
; see Pydantic Types for other custom IP address types -
enum.Enum
checks that the value is a valid Enum instance
-
subclass of enum.Enum
checks that the value is a valid member of the enum; see Enums and Choices for more details
-
enum.IntEnum
checks that the value is a valid IntEnum instance
-
subclass of enum.IntEnum
checks that the value is a valid member of the integer enum; see Enums and Choices for more details
-
decimal.Decimal
pydantic attempts to convert the value to a string, then passes the string to
Decimal(v)
-
pathlib.Path
simply uses the type itself for validation by passing the value to
Path(v)
; see Pydantic Types for other more strict path types -
uuid.UUID
strings and bytes (converted to strings) are passed to
UUID(v)
, with a fallback toUUID(bytes=v)
forbytes
andbytearray
; see Pydantic Types for other stricter UUID types -
ByteSize
converts a bytes string with units to bytes